<?php

class Faq extends CActiveRecord
{
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return '{{faq}}';
	}

	public function rules()
	{
		$languages = Languages::model()->getLanguageCodes();
		
		$required = 'active';
		foreach ($languages as $lang)
		{
			$required .= ', question_'.$lang.', answer_'.$lang;
		}
		$length = '';
		foreach ($languages as $lang)
		{
			$length .= ', question_'.$lang;
		}
		$length = rtrim($length, ',');
		$search = 'id, active, sorter, date_updated';
		foreach ($languages as $lang)
		{
			$search .= ', question_'.$lang.', answer_'.$lang;
		}
		return array(
			array($required, 'required'),
			array('active, sorter', 'numerical', 'integerOnly'=>true),
			array($length, 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array($search, 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
		);
	}

	public function attributeLabels()
	{
		$labels = array(
			'id' => 'ID',
			'active' => tt('Active'),
			'sorter' => tt('Sorter'),
		);
		
		$languages = Languages::model()->getLanguageCodes();
		foreach($languages as $lang)
		{
			$labels['question_'.$lang] = tt('Question');
			$labels['answer_'.$lang] = tt('Answer');
		}
		return $labels;
	}

	public function search()
	{
		$criteria=new CDbCriteria;

		$languages = Languages::model()->getLanguageCodes();
		foreach ($languages as $lang)
		{
			$criteria->compare('question_'.$lang,$this->{'question_'.$lang},true);
			$criteria->compare('answer_'.$lang,$this->{'answer_'.$lang},true);
		}
		$criteria->order = 'sorter ASC';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>param('adminPaginationPageSize', 20),
			),
		));
	}
	
	public function beforeSave()
	{
		if($this->isNewRecord)
		{
			$this->active = 1;

			$maxSorter = Yii::app()->db->createCommand()
				->select('MAX(sorter) as maxSorter')
				->from($this->tableName())
				->queryRow();
			$this->sorter = $maxSorter['maxSorter']+1;
		}
		
		$languages = Languages::model()->getLanguageCodes();
		foreach ($languages as $lang)
		{
			if (empty($this->{'question_'.$lang}))
			{
				$this->{'question_'.$lang} = $this->{'question_'.Settings::model()->get('localization', 'defaultLang')};
			}
			
			$this->{'answer_'.$lang} = preg_replace("/(.*)\r\n<br>$/", "$1", $this->{'answer_'.$lang});
			if (empty($this->{'answer_'.$lang}))
			{
				$this->{'answer_'.$lang} = $this->{'answer_'.Settings::model()->get('localization', 'defaultLang')};
			}
		}
		return parent::beforeSave();
	}
	
	public function getMinId()
	{
		$minId = Yii::app()->db->createCommand()
			->select('MIN(id) as minId')
			->from($this->tableName())
			->queryRow();
		return $minId['minId'];
	}
	
	public function getViewUrl($mode = '')
	{
		$route = (empty($mode)) ? '/faq/default/view' : '/faq/admin/default/view';
		return Yii::app()->createUrl($route, array( 'id'=>$this->id ));
	}
}